# from datetime import datetime
#
# money=float(input("请输入您的卡余额"))
# shopping_list=[]
# shopping_prices=[]
# goods = {
#     "苹果": 5,
#     "香蕉": 3,
#     "橙子": 4,
#     "西瓜": 2,
#     "草莓": 15,
#     "西红柿": 3,
#     "黄瓜": 2,
#     "土豆": 1.5,
#     "白菜": 1,
#     "猪肉": 25,
#     "牛肉": 45,
#     "鸡肉": 18,
#     "鸡蛋": 10,
#     "牛奶": 6,
#     "面包": 8,
#     "大米": 4,
#     "面粉": 3,
#     "牙膏": 12,
#     "洗发水": 35,
#     "卫生纸": 20
# }
#
# if money <=0:
#     print("没钱")
#
# else:
#     print("欢迎光临XX超市")
#
# temp_money = money
# while True:
#     print(goods) #展示商品
#     shop=str(input("请输入你想要的商品"))
#
#     if shop not in goods:
#         print("很抱歉，该超市没有"+shop)
#     elif temp_money<goods[shop]:
#         print("抱歉，余额不足，无法购买"+shop)
#     else:
#         price=goods[shop]
#         shopping_list.append(shop)  # 添加商品名称
#         shopping_prices.append(price)  # 添加对应价格
#         temp_money=temp_money-price
#
#         print("已添加购物车")
#
#     while True:
#         choice = input("是否继续购物？(y/q): ").strip().lower()
#         if choice in ['y', 'q']:
#             break
#         print("输入无效，请输入'y'或'q'！")
#
#     if choice == 'q':
#         total=sum(shopping_prices) #购物总价
#         balance = money - total #购物完成，卡余额
#         print("购物结束！")
#
#         #输出购买的商品
#         for item in shopping_list:
#             print(item)
#
#         #输出购买了几件商品
#         count = len(shopping_list)
#         print(f"共{count}件商品")
#
#         #打印时间
#         now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#         print(now)
#         #打印总价
#         print("合计："+str(total)+"元")
#
#         if balance>=0:
#             print("购物成功您的卡余额还剩"+str(balance))
#         else:
#             print("您的余额不足，无法购买")
#         break
from collections import Counter
from datetime import datetime


# 退回商品
def shopping_back(temp_money, shopping_list, shopping_prices):
    while True:
        choice = input("是否有要退回商品？(y/n):").strip().lower()
        if choice in ['y', 'n']:
            break
        print("输入无效，请输入'y'或'n'！")
    if choice == 'y':
        shop = input("请输入要退回商品：")
        if shop not in shopping_list:
            print(f"购物车内未找到商品 '{shop}'！")

        index=shopping_list.index(shop)
        price_back=goods[shop]
        # 移除商品和对应的价格
        shopping_list.pop(index)
        shopping_prices.remove(price_back)
        temp_money=temp_money+price_back
    return temp_money, shopping_list, shopping_prices



money = float(input("请输入您的卡余额"))
shopping_list = []
shopping_prices = []
goods = {
    "苹果": 5,
    "香蕉": 3,
    "橙子": 4,
    "西瓜": 2,
    "草莓": 15,
    "西红柿": 3,
    "黄瓜": 2,
    "土豆": 1.5,
    "白菜": 1,
    "猪肉": 25,
    "牛肉": 45,
    "鸡肉": 18,
    "鸡蛋": 10,
    "牛奶": 6,
    "面包": 8,
    "大米": 4,
    "面粉": 3,
    "牙膏": 12,
    "洗发水": 35,
    "卫生纸": 20
}

if money <= 0:
    print("没钱")

else:
    print("欢迎光临XX超市")

temp_money = money

while True:
    print(goods)  # 展示商品
    shop = str(input("请输入你想要的商品"))

    if shop not in goods:
        print("很抱歉，该超市没有" + shop)
    elif temp_money < goods[shop]:
        print("抱歉，余额不足，无法购买" + shop)
    else:
        price = goods[shop]
        shopping_list.append(shop)  # 添加商品名称
        shopping_prices.append(price)  # 添加对应价格
        temp_money = temp_money - price
        print("已添加购物车")
        print(f"目前购物车里有{shopping_list}")  # 输出目前已添加的商品
        shopping_back(price, shopping_list, shopping_prices)
    while True:

        choice = input("是否继续购物？(y/q): ").strip().lower()
        if choice in ['y', 'q']:
            break
        print("输入无效，请输入'y'或'q'！")

    if choice == 'q':
        total = sum(shopping_prices)  # 购物总价
        balance = money - total  # 购物完成，卡余额
        print("购物结束！")

        # 输出购买的商品
        counts = Counter(shopping_list)  # 字典类型 Counter({'土豆': 4, '面粉': 1})
        for item, count in counts.items():
            print(f"{item}*{count}")

        # 输出购买了几件商品
        count = len(shopping_list)
        print(f"共{count}件商品")

        # 打印时间
        now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print(now)
        # 打印总价
        print("合计：" + str(total) + "元")

        if balance >= 0:
            print("购物成功您的卡余额还剩" + str(balance))
        else:
            print("您的余额不足，无法购买")
        break

